package com.sc.nft.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.InformationArticle;
import com.sc.nft.entity.vo.AppArticleVO;
import com.sc.nft.entity.vo.AppByIdArticleVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.mapper.InformationArticleMapper;
import com.sc.nft.sup.PageRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class InformationArticleDao extends ServiceImpl<InformationArticleMapper, InformationArticle> {

    /**
     * 根据aid,是否存在,新增数据
     * 数据不存在,新增 存在则不新增
     *
     * @param informationArticle
     * @return
     */
    public Boolean selectByAidSaveObject(InformationArticle informationArticle) {
        Long aId = informationArticle.getAId();
        QueryWrapper<InformationArticle> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("a_id", aId);
        List<InformationArticle> list = list(queryWrapper);
        if (CollUtil.isEmpty(list)) {
            baseMapper.save(informationArticle);
        }
        return Boolean.TRUE;
    }

    public List<AppArticleVO> getArticleList(PageRequest pageRequest) {
        Integer pageNo = pageRequest.getPageNo();
        Integer pageSize = pageRequest.getPageSize();
        if (pageNo > 0) {
            pageNo = (pageNo - 1) * pageSize;
        } else {
            throw new GlobalRunTimeException("页码异常");
        }
      return baseMapper.getArticleList(pageNo, pageSize);
    }


    public AppByIdArticleVO appSelectById(Long id){
        return baseMapper.appSelectById(id);
    }
    /**
     * 获取最新5条,抛出当前的条目
     *
     * @param id
     * @return
     */
    public List<AppByIdArticleVO> getLimitFive(Long id){
        return baseMapper.getLimitFive(id);
    }
}